package com.sufen.common.utils;

import com.sufen.common.enums.DateTypeEnum;
import com.sun.istack.internal.NotNull;
import com.sun.istack.internal.Nullable;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @description: 与事件相关的工具类
 * @function: 此工具类主要提供日期的浮动以及日期字符串与时间戳的装换
 * @author: Liu Menglei
 * @Date: 2021/12/4 0004 09:23
 */
public final class DateTimeUtilsD extends CommonUtils{
    private static final Logger logger = LoggerFactory.getLogger(DateTimeUtilsD.class);
    /**
    *@Description: 根据两个默认的时间日期格式计算两个时间的差值
    *@Param: 时间1，时间2，返回值类型
    *@return: 相差日期的时间格式表示
    *@date: 2021/12/5 0005
    */
    public static String getTimeDiffValue(@NotNull String time1,@NotNull String time2,
                                          @NotNull DateTypeEnum paramType,@NotNull SpanType resultType){
        String diffValue = "";Date date1 = null;Date date2 = null;SimpleDateFormat sdf;
        //参数合法性校验
        if (StringUtils.isEmpty(time1)||StringUtils.isEmpty(time2))return diffValue;
        //校验类型参数是否为空，为空的赋默认值
        sdf = new SimpleDateFormat(paramType.getFormatTemplate());
        try{
            date1 = sdf.parse(time1);
            date2 = sdf.parse(time2);
        }catch (Exception e){
            logger.error(e.getMessage());
        }
        return getTimeDiffValue(date1,date2,resultType);
    }

    public static String getTimeDiffValue(@NotNull String time1,@NotNull String time2){
        return getTimeDiffValue(time1,time2, DateTypeEnum.DATE_TIME_DEFAULT,SpanType.DIFF_VALUE_MILL);
    }

    public static String getTimeDiffValue(@NotNull Date time1, @NotNull Date time2){
        return getTimeDiffValue(time1,time2,SpanType.DIFF_VALUE_MILL);
    }

    public static String getTimeDiffValue(@NotNull Date time1, @NotNull Date time2, SpanType resultType){
        String diffValue = "";
        if (time1==null||time2==null)return diffValue;
        long dc = Math.abs(time1.getTime()-time2.getTime());
        if (resultType==SpanType.TIME_SPAN_SECS){
            diffValue = ""+(dc/1000);
        }else if (resultType==SpanType.TIME_SPAN_VISUAL){
            diffValue = timestamp2String(dc);
        }else{
            diffValue = ""+dc;
        }
        return diffValue;
    }

    /**
    *@Description: 对时间进行加减运算
    *@Param:
    *@return:
    *@date: 2021/12/7 0007
    */
    public static String dateFloatByCount(String oldDate,long floatCount,DateTypeEnum paramType){

        return null;
    }



    /**
    *@Description: 将精确到毫秒的时间长度转换为有文字表示的时间
    *@Param: 时间长度
    *@return: 文字表示的时间长度
    *@date: 2021/12/5 0005
    */
    public static String timestamp2String (long timestamp){
        String resultTimeSpan = "";
        if (timestamp<=0)return resultTimeSpan;
        //将时间戳毫秒级别的是时间转换为秒级别的时间戳
        timestamp = timestamp/1000;
        long date = timestamp/(24*60*60);
        long hour = (timestamp-date*24*60*60)/(60*60);
        long minutes = (timestamp-date*24*60*60-hour*60*60)/(60);
        long second = (timestamp-date*24*60*60-hour*60*60-minutes*60);
        return (date==0?"":(date+"天"))+(hour==0?"":(hour+"小时"))+
                (minutes==0?"":(minutes+"分"))+(second==0?"":(second+"秒"));
    }

    /**
    *@Description: 将字符串转换为日期对象
    *@Param:
    *@return:
    *@date: 2021/12/5 0005
    */
    public static Date string2Date(String dateVar,DateTypeEnum dateType) throws ParseException {
        return new SimpleDateFormat(dateType.getFormatTemplate()).parse(dateVar);
    }

    /**
    *@Description: 此方法应该作为一个多态方法，提供多种不同类型的日期转换
    *@Param:
    *@return:
    *@date: 2021/12/4 0004
    */
    public static String dateFloatDay(@NotNull String oldDate, @Nullable long floatDate, DateTypeEnum dateType){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateType.getFormatTemplate());
        Date time = null;
        try {
            Date date = simpleDateFormat.parse(oldDate);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH, (int) floatDate);
            time = calendar.getTime();
        }catch (Exception e){
            logger.error(e.getMessage());
        }
        return simpleDateFormat.format(time);
    }




    public Date string2Date(String sourceDate){
        Date date = null;
        return date;
    }

    static enum SpanType{
        TIME_SPAN_VISUAL("字符串格式时间长度","visual"),
        TIME_SPAN_SECS("秒级表示的时间长度","SECS"),
        DIFF_VALUE_MILL("毫秒表示时间差","millisecond");
        private String typeName;
        private String typeStr;
        SpanType(String typeName, String typeStr) {
            this.typeName = typeName;
            this.typeStr = typeStr;
        }
        public String getTypeStr() {
            return typeStr;
        }
    }

    static enum FloatType{
        ;

        private String floatName;
        private String floatStr;

        FloatType(String floatName) {
            this.floatName = floatName;
        }
    }

    @Test
    public void testApplication(){
        System.out.println(DateTypeEnum.DATE_YEAR.getFormatTemplate());
        //验证字符串是否能够转换为long
        String d = "1111111";
        System.out.println(Long.parseLong(d));
        System.out.println();
        //验证时间装换的工具
        String time1 = "2015-10-21 19:18:22 123";
        String time2 = "2015-12-21 10:38:21 323";
        System.out.println(getTimeDiffValue(time1,time2,DateTypeEnum.DATE_TIME_FULL,SpanType.TIME_SPAN_VISUAL));
    }
}